04. Python Lists

Python Lists

Question:

Python has an interesting data stucture called a "list" that is much more than a mere list. In fact, a Python list actually encompasses the functionality of almost every list-based data structure in this lesson.

Behind the scenes a Python list is built as an array. Even though you can do many operations on a Python list with just one line of code, there's a lot of code built in to the Python language running to make that operation possible.

For example, inserting into a list is easy (happens in constant time). However, inserting into an array is O(n), since you may need to shift elements to make space for the one you're inserting, or even copy everything to a new array if you run out of space. Thus, inserting into a Python list is actually O(n), while operations that search for an element at a particular spot are O(1). You can see the runtime of other list operations here .

Python is a "higher level" programming language, so you can accomplish a task with little code. However, there's a lot of code built into the infrastructure in this way that causes your code to actually run much more slowly than you'd think. Keep this in the back of your mind when using Python. You likely won't need to know the details of how Python works behind the scenes in a programming interview, but you'll seem very impressive if you do!

If you aren't already comfortable with Python lists, you can look through this lesson about basic Python list manipulation.

Start Quiz:

Solution:

Good job! The answer was in the documentation from the quiz introduction. Keep in mind that Python has a lot of built-in optimizations—with a normal list, you might need to traverse through every element to find the length, but Python does some work behind the scenes to get you the list length in constant time!

INSTRUCTOR NOTE:

Note: Specifically this is the runtime of finding the length of a list using the len() function